<template>
  <div class="login-container">
    <div class="login-box">
      <img class="login-left" src="@/assets/img/login-left.png" alt="">
      <div class="login-right">
        <div class="login-head">
          <img class="login-logo" src="@/assets/img/logo.png" alt="">
          <div class="login-name">如是观影企业版后台管理</div>
        </div>
        <el-tabs v-model="activeName" class="login-tab" @tab-click="handleClick">
          <el-tab-pane label="用户密码登录" name="first">
            <el-form ref="loginForm" :model="loginForm" :rules="loginRules">
              <el-form-item prop="username">
                <el-input v-model="loginForm.phone" class="user-phone" placeholder="输入手机号码" />
              </el-form-item>
              <el-form-item prop="password">
                <el-input v-model="loginForm.pwd" class="user-phone" placeholder="输入密码" />
              </el-form-item>
              <el-form-item prop="code">
                <div class="user-phone">
                  <el-input v-model="loginForm.code" placeholder="输入验证码" />
                  <img class="login-code" src="@/assets/img/login-code.png" alt="">
                </div>
              </el-form-item>
              <div class="login-btn" @click="handleLogin">登录</div>
              <div class="login-tips">
                <div class="tips-left">没有账号？立即</div>
                <div class="tips-right" @click="register">免费注册</div>
              </div>
            </el-form>
          </el-tab-pane>
          <el-tab-pane label="手机验证码登录" name="second">
            <el-form ref="loginForm" :model="loginForm" :rules="loginRules">
              <el-form-item prop="username">
                <el-input v-model="loginForm.phone" class="user-phone" placeholder="输入手机号码" />
              </el-form-item>
              <el-form-item prop="password">
                <div class="user-phone">
                  <el-input v-model="loginForm.code" placeholder="输入验证码" />
                  <div class="code-btn">获取验证码</div>
                </div>
              </el-form-item>
              <div class="login-btn" @click="handleLogin">登录</div>
              <div class="login-tips">
                <div class="tips-left">没有账号？立即</div>
                <div class="tips-right" @click="register">免费注册</div>
              </div>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </div>
    </div>
    <!-- <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">

      <div class="title-container">
        <h3 class="title">Login Form</h3>
      </div>

      <el-form-item prop="username">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <el-input
          ref="username"
          v-model="loginForm.username"
          placeholder="Username"
          name="username"
          type="text"
          tabindex="1"
          auto-complete="on"
        />
      </el-form-item>

      <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input
          :key="passwordType"
          ref="password"
          v-model="loginForm.password"
          :type="passwordType"
          placeholder="Password"
          name="password"
          tabindex="2"
          auto-complete="on"
          @keyup.enter.native="handleLogin"
        />
        <span class="show-pwd" @click="showPwd">
          <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
        </span>
      </el-form-item>

      <el-button :loading="loading" type="primary" style="width:100%;margin-bottom:30px;" @click.native.prevent="handleLogin">Login</el-button>

      <div class="tips">
        <span style="margin-right:20px;">username: admin</span>
        <span> password: any</span>
      </div>

    </el-form> -->
  </div>
</template>

<script>
import { validUsername } from '@/utils/validate'

export default {
  name: 'Login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!validUsername(value)) {
        callback(new Error('Please enter the correct user name'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('The password can not be less than 6 digits'))
      } else {
        callback()
      }
    }
    return {
      activeName: 'first',
      loginForm: {
        username: 'admin',
        password: '111111',
        code: ''
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
    handleClick() {},
    register() {
      this.$router.push({ path: '/register' })
    },
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('user/login', this.loginForm).then(() => {
            this.$router.push({ path: this.redirect || '/' })
            this.loading = false
          }).catch(() => {
            this.loading = false
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg:#283443;
$light_gray:#fff;
$cursor: #fff;

</style>

<style lang="scss" scoped>
@import "~@/styles/variables.scss";

$bg:#2d3a4b;
$dark_gray:#889aa4;
$light_gray:#eee;

.login-container {
  min-height: 100%;
  min-width: 1200px;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #F3F6FD;
  .login-box{
    width: 1200px;
    height: 718px;
    padding: 60px 0;
    display: flex;
    justify-content: space-between;
    border-radius: 20px;
    background-color: #FFFFFF;
    box-shadow: 0px 0px 60px rgba(152, 152, 152, 0.2);
    .login-left{
      width: 600px;
      height: 597px;
    }
    .login-right{
      padding-top: 24px;
      padding-right: 104px;
      .login-head{
        display: flex;
        align-items: center;
        padding-bottom: 48px;
        .login-logo{
          width: 64px;
          height: 64px;
          margin-right: 28px;
        }
        .login-name{
          color: #001161;
          font-size: 28px;
          font-weight: bold;
          line-height: 40px;
        }
      }
      ::v-deep .login-tab{
        .el-tabs__content{
          overflow: inherit;
        }
        .el-tabs__nav{
          width: 400px;
          display: flex;
          align-items: center;
          justify-content: space-between;
          box-sizing: border-box;
          .el-tabs__item{
            height: 28px;
            color: #707070;
            font-size: 20px;
            line-height: 28px;
            padding: 0;
          }
          .el-tabs__item.is-active{
            color: #020002;
            font-weight: bold;
            height: 40px;
            line-height: 40px;
            font-size: 28px;
          }
        }
        .el-tabs__header{
          margin: 0;
        }
        .el-tabs__active-bar{
          height: 0;
        }
        .el-tabs__nav-wrap::after{
          content: "";
          height: 0;
        }
        .el-form-item{
          margin: 0;
          .user-phone{
            position: relative;
            display: flex;
            margin-top: 28px;
            .el-input__inner{
              height: 56px;
              line-height: 56px;
              padding: 0 20px;
              border-radius: 8px;
              border: 1px solid #f5f5f5;
              background-color: #FFFFFF;
              box-shadow: 0px 0px 10px rgba(112, 112, 112, 0.1);
            }
            .login-code{
              width: 137px;
              height: 56px;
              margin-left: 28px;
            }
            .code-btn{
              position: absolute;
              top: 17px;
              right: 20px;
              cursor: pointer;
              color: $bodyColor;
              font-size: 16px;
              line-height: 22px;
            }
          }
        }
        .login-btn{
          height: 56px;
          font-size: 20px;
          cursor: pointer;
          color: #FFFFFF;
          margin-top: 28px;
          line-height: 56px;
          text-align: center;
          border-radius: 8px;
          background-color: $bodyColor;
          box-shadow: 0px 0px 10px rgba(112, 112, 112, 0.1);
        }
        .login-tips{
          display: flex;
          align-items: center;
          justify-content: center;
          padding-top: 24px;
          .tips-left{
            color: #747474;
            font-size: 18px;
            line-height: 25px;
            padding-right: 4px;
          }
          .tips-right{
            color: $bodyColor;
            font-size: 18px;
            font-weight: bold;
            line-height: 25px;
            cursor: pointer;
          }
        }
      }
    }
  }
}
</style>
